package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("增加菜品")
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result add(@RequestBody DishDTO dishDTO){
        dishService.add(dishDTO);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    @DeleteMapping
    @ApiOperation("删除菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public  Result delete(@RequestParam List<Long> ids){
        dishService.delete(ids);
        //涉及到多个缓冲全部删除
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查菜品")
    public Result getbyid(@PathVariable Long id){
        return Result.success(dishService.getbyid(id));
    }
    @GetMapping("/list")
    @ApiOperation("根据分类查菜品")
    public Result<List <DishVO>> getbycategoryid(Long categoryId){
        return Result.success(dishService.getbycategoryId(categoryId));
    }
    @PostMapping("/status/{status}")
    @ApiOperation("起售和停售菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result startend(@PathVariable Integer status,Long id){
        dishService.startend(status,id);
        //涉及到多个缓冲全部删除可能修改分类
        return Result.success();
    }
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO){
        dishService.update(dishDTO);
        //涉及到多个缓冲全部删除可能修改分类

        return Result.success();
    }

}
